package com.example.zuul.ratelimit;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangjianrong
 * @date 2020/7/10 16:22
 * 限流提示
 **/
@RestController
public class ExceptionHandler implements ErrorController {
//    @Override
//    public String getErrorPath() {
//       // return "/error";
//        return "{\"result\":\"访问太多频繁，请稍后再访问！！！\"}";
//    }
//
//    @RequestMapping(value="/error")
//    public String error(){
//        return "{\"result\":\"访问太多频繁，请稍后再访问！！！\"}";
//    }
@RequestMapping("/error")
public String handleError(HttpServletRequest request){
    //获取statusCode:401,404,500
    Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
  //  System.err.println("code:" + statusCode);
    if(statusCode == 401){
        return "/401";
    }else if(statusCode == 404){
        return "/404";
    }else if(statusCode == 403){
        return "/403";
    }else if(statusCode == 500){
        return "/500";
    }else if(statusCode == 429){
        return "{\"result\":\"访问太多频繁，请稍后再访问！！！\"}";
    }
    return String.valueOf(statusCode);
}
    @Override
    public String getErrorPath() {
        return "/error";
    }
}
